package com.yssoft.service.core;


import java.util.*;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TArea;
import com.yssoft.entity.core.TIndustryCategory;
import com.yssoft.mapper.core.TAreaMapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
@Service
public class AreaService {
    @Resource
    protected TAreaMapper areaMapper;
    
    /**
     * @param id
     * @return TArea
     * @方法名称: queryRoleById
     * @功能描述: 获取唯一区域数据
     * @作者: 洪丰
     * @创建时间: 2018年4月26日 下午3:13:45
     */
    public TArea queryRoleById(Integer id) {
        return areaMapper.selectByPrimaryKey(id);
    }
    
    /**
     * @方法名称: queryPage
     * @功能描述: 分页查询列表数据
     * @作者:洪丰
     * @创建时间: 2018年4月25日 下午5:49:05
     */
//	public String queryDeptListByPropertity(){
//		List<TArea> aLists = areaMapper.selectAreaAndChildren();
//		StringBuffer sb = new StringBuffer();
//		sb.append("[");
//		int i = 0;
//		for (TArea ic : aLists) {
//
//			if (i != 0) {
//				sb.append(",");
//			}
//			if (ic.getT_a_id() != null) {
//				sb.append("{id:" + ic.getId() + "',pid:" + ic.getT_a_id() + "',name:" + ic.getName() + "',code:" + ic.getCode());
//				if (ic.getChildrenList().size() > 0) {
//					sb.append(",_children: true");
//				}
//				sb.append("}");
//			} else {
//				sb.append("{id:" + ic.getId() + "',pid:0" + "',name:" + ic.getName() + "',code:" + ic.getCode());
//				if (ic.getChildrenList().size() > 0) {
//					sb.append(",_children: true");
//				}
//				sb.append("}");
//			}
//			i++;
//		}
//		sb.append("]");
//		return sb.toString();
//	}
    
    /**
     * 查询区域tableTree使用
     *
     * @return
     */
    public List<Map<String, Object>> queryAreaTableTree() {
        
        return areaMapper.queryArea();
        
    }
    
    /**
     * @param area void
     * @方法名称: addAcc
     * @功能描述: 新增区域
     * @作者: 洪丰
     * @创建时间: 2018年4月26日 下午3:14:02
     */
    public void addAcc(TArea area) {
        areaMapper.insert(area);
    }
    
    /**
     * @param entity void
     * @方法名称: editAcc
     * @功能描述: 修改区域
     * @作者: 洪丰
     * @创建时间: 2018年4月26日 下午3:14:13
     */
    public void editAcc(TArea entity) {
        areaMapper.updateByPrimaryKeySelective(entity);
    }
    
    /**
     * @param id
     * @return int
     * @方法名称: delAccById
     * @功能描述: 逻辑删除区域
     * @作者: 洪丰
     * @创建时间: 2018年4月26日 下午3:14:22
     */
    public int delAccById(Integer id) {
        TArea area = areaMapper.selectByPrimaryKey(id);
        area.setDelete_flag(1);
        return areaMapper.updateByPrimaryKeySelective(area);
    }
    
    /**
     * @return
     * @功能描述:查询区域树数据
     * @作者:戈超
     */
    public String queryAreaTree() {
        /*获取区域一级节点*/
        List<Map<String, Object>> node = areaMapper.queryAreaTreeOneNode();
        /*获取全部数据*/
        List<Map<String, Object>> areaList = areaMapper.queryArea();
        for (Map<String, Object> area : node) {
            List<Map<String, Object>> children = queryAreaTreeNode(area.get("id").toString(), areaList);
            if (children.size() > 0) {
                area.put("children", children);
            }
        }
        
        String jsonArray = JSONObject.toJSONString(node);
        return jsonArray;
    }
    
    /**
     * @param areaId
     * @param areaList
     * @return
     * @功能描述:递归查询一级区域下树形数据
     * @作者:戈超
     */
    private List<Map<String, Object>> queryAreaTreeNode(String areaId, List<Map<String, Object>> areaList) {
        List<Map<String, Object>> areaNode = new ArrayList<>();
        for (Map<String, Object> map : areaList) {
            if (areaId.equals(map.get("pid").toString())) {
                List<Map<String, Object>> children = queryAreaTreeNode(map.get("id").toString(), areaList);
                if (children.size() > 0) {
                    map.put("children", children);
                }
                areaNode.add(map);
            }
        }
        return areaNode;
    }
    
    
    /**
     * @param id
     * @return
     * @功能描述:根据id获取实体
     * @作者:戈超
     */
    public TArea queryAreaById(Integer id) {
        TArea area = areaMapper.queryAreaById(id);
        return area;
    }
    
    /**
     * @param id
     * @return
     * @功能描述:递归删除区域
     * @作者:王永宁
     */
    @Transactional(rollbackFor = Exception.class)
    public int delIndustryCategory(String areaId) {
        List<TArea> childArea = areaMapper.queryAreaChildren(areaId);
        List<Integer> idList = new ArrayList<>();
        for (TArea area : childArea) {
            idList.add(area.getId());
        }
        return areaMapper.deleteArea(idList);
    }
    
    /**
     * 区域新增tree
     *
     * @return
     */
    public List<Map<String, Object>> queryAreaNewTree() {
        List<Map<String, Object>> areaList = null;
        areaList = areaMapper.queryAllAreaForAdd();
        Iterator<Map<String, Object>> it = areaList.iterator();
        List<Integer> genList = new ArrayList<>();
        while (it.hasNext()) {
            Map<String, Object> map1 = it.next();
            if (map1.get("pid") == null) {
                genList.add(Integer.parseInt(map1.get("id") + ""));
                for (Map<String, Object> map2 : areaList) {
                    if (map2.get("pid") != null && map2.get("pid").equals(map1.get("id"))) {
                        List<Map<String, Object>> childrens = (List<Map<String, Object>>) map1.get("children");
                        if (childrens == null) {
                            childrens = new ArrayList<>();
                        }
                        childrens.add(map2);
                        map1.put("children", childrens);
                    }
                }
            } else {
                for (Map<String, Object> map2 : areaList) {
                    if (map2.get("id").equals(map1.get("pid")) && map2.get("pid") != null) {
                        List<Map<String, Object>> childrens = (List<Map<String, Object>>) map2.get("children");
                        if (childrens == null) {
                            childrens = new ArrayList<>();
                        }
                        childrens.add(map1);
                        map2.put("children", childrens);
                    }
                }
            }
        }
        Iterator<Map<String, Object>> it2 = areaList.iterator();
        while (it2.hasNext()) {
            Map<String, Object> map = it2.next();
            if (map.get("pid") != null) {
                it2.remove();
            }
        }
        return areaList;
    }
    
    /**
     * 查询子地区
     *
     * @param provinceId
     * @return
     */
    public List<Map<String, Object>> queryChildren(String provinceId) {
        List<Map<String, Object>> areaList = areaMapper.queryMapAreaChildren(provinceId);
        List<Map<String, Object>> children = queryAreaIndexTreeNode(provinceId, provinceId, areaList);
        System.out.println(children);
        return children;
    }
    
    /**
     * @方法名: queryAreaIndexTreeNode
     * @创建人: 陈翼东
     * @时间: 2018/6/15 9:03
     * @参数: [areaId, areaList]
     * @返回值: java.util.List<java.util.Map   <   java.lang.String   ,   java.lang.Object>>
     * @说明: 生成首页需要的格式数据
     */
    private List<Map<String, Object>> queryAreaIndexTreeNode(String parentId, String areaId, List<Map<String, Object>> areaList) {
        List<Map<String, Object>> areaNode = new ArrayList<>();
        Iterator<Map<String, Object>> areaIterator = areaNode.iterator();
        while (areaIterator.hasNext()){
            Map<String, Object> areaMap = areaIterator.next();
            if(parentId.equals(areaMap.get("id"))){
                areaIterator.remove();
            }
        }
        
        for (Map<String, Object> map : areaList) {
           
            if (parentId.equals(map.get("pid").toString())) {
                map.put("name", map.get("NAME"));
//                map.put("iconOpen", "");
//                map.put("iconClose", "");
            } else {
                map.put("name", "-"+map.get("NAME"));
//                map.put("icon", "-");
            }
            if (areaId.equals(map.get("pid").toString())) {
                List<Map<String, Object>> children = queryAreaIndexTreeNode(parentId, map.get("id").toString(), areaList);
                if (children.size() > 0) {
                    map.put("children", children);
                }
                areaNode.add(map);
            }
        }
        return areaNode;
    }
    
}
